package com.renren.project.db.validator;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationUtils {

	/*手机号格式验证*/
	private static final Pattern mobile_pattern = Pattern.compile("^1[23456789][0-9]{9}$");

	/**
	 * 登录账号验证
	 * 必须为字母与数字的组合
	 */
	//private static final Pattern account_pattern = Pattern.compile("[0-9]+[a-zA-Z]+[0-9a-zA-Z]*|[a-zA-Z]+[0-9]+[0-9a-zA-Z]*");

	//type=0  4-10位不能有特殊符号下划线
	private static final Pattern account_pattern = Pattern.compile("^[a-zA-Z0-9]{4,10}$");

	//type=1 4-10为不能有特殊符号可以有下划线
	private static final Pattern pattern = Pattern.compile("^[\\w]{4,10}$");

	public static boolean isAcconut(String value,Integer type) {
		switch (type){
			case 0:
				return account_pattern.matcher(value).matches();
			case 1:
				return pattern.matcher(value).matches();
			default:
				return account_pattern.matcher(value).matches();
		}

	}

	//密码正确格式为：不能包含空格、回车、换行、中文字符，其他都可以，长度6到12位
	private static final Pattern password_pattern = Pattern.compile("^[^\\s\\u4e00-\\u9fa5]{6,12}$");
	public static boolean isPassword(String src) {
		if(StringUtils.isEmpty(src)) {
			return false;
		}
		Matcher m = password_pattern.matcher(src);
		return m.matches();
	}

	public static boolean isPhone(String src) {
		if(StringUtils.isEmpty(src)) {
			return false;
		}
		Matcher m = mobile_pattern.matcher(src);
		return m.matches();
	}
}
